package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.entity.BtyyCwPzxq;
import cn.stylefeng.guns.sys.modular.system.entity.BtyyCwSyfs;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyyCwPzxqMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyyCwSyfsMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.CwbbParam;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Service
public class BtyPzxqService extends ServiceImpl<BtyyCwPzxqMapper, BtyyCwPzxq> {
    @Resource
    private BtyyCwPzxqMapper pzxqMapper;

    /**
     * 获取辅助核算余额列表
     */
    public Page<Map<String, Object>> list(CwbbParam cwbbParam) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.list(page,cwbbParam);
    }
    /**
     * 获取凭证详情列表（不分页）
     */
    public List<BtyyCwPzxq> listExportPzxq(CwbbParam cwbbParam) {

        return this.baseMapper.listExportPzxq(cwbbParam);
    }
    /**
     * 导入凭证详情excel
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchImportPzxq(String fileName, MultipartFile file,String dm) throws Exception {
        boolean notNull = false;
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            throw new Exception("上传文件格式不正确");
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }

        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        //excel中多个sheet表，获取所有sheet表的数量
        //int sheets = wb.getNumberOfSheets();
        for (int i = 0; i < 1; i++) {
            Sheet sheet = wb.getSheetAt(i);
            if (sheet != null) {
                notNull = true;
            }

            int lastRowNum = sheet.getLastRowNum();
            for (int r = 2; r <= lastRowNum; r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                for(int m=0;m<33;m++){
                    Cell cell = row.getCell(m);
                    if (ToolUtil.isNotEmpty(cell)) {
                        cell.setCellType(CellType.STRING);
                    }
                }
                String zdrq = row.getCell(0) == null ? "" : row.getCell(0).getStringCellValue();
                String pzlb = row.getCell(1) == null ? "" : row.getCell(1).getStringCellValue();
                String pzbh = row.getCell(2) == null ? "" : row.getCell(2).getStringCellValue();
                String fjzs = row.getCell(3) == null ? "" : row.getCell(3).getStringCellValue();
                String cjr = row.getCell(4) == null ? "" : row.getCell(4).getStringCellValue();
                String xh = row.getCell(5) == null ? "" : row.getCell(5).getStringCellValue();
                String kmbh = row.getCell(6) == null ? "" : row.getCell(6).getStringCellValue();
                String kmmc = row.getCell(7) == null ? "" : row.getCell(7).getStringCellValue();
                String zy = row.getCell(8) == null ? "" : row.getCell(8).getStringCellValue();
                String jfje = row.getCell(9) == null ? "" : row.getCell(9).getStringCellValue();
                String dfje = row.getCell(10) == null ? "" : row.getCell(10).getStringCellValue();
                String fzhs = row.getCell(11) == null ? "" : row.getCell(11).getStringCellValue();
                String mc = row.getCell(12) == null ? "" : row.getCell(12).getStringCellValue();
                String fzhs2 = row.getCell(13) == null ? "" : row.getCell(13).getStringCellValue();
                String mc2 = row.getCell(14) == null ? "" : row.getCell(14).getStringCellValue();
                String fzhs3 = row.getCell(15) == null ? "" : row.getCell(15).getStringCellValue();
                String mc3 = row.getCell(16) == null ? "" : row.getCell(16).getStringCellValue();
                String fzhs4 = row.getCell(17) == null ? "" : row.getCell(17).getStringCellValue();
                String mc4 = row.getCell(18) == null ? "" : row.getCell(18).getStringCellValue();
                String fzhs5 = row.getCell(19) == null ? "" : row.getCell(19).getStringCellValue();
                String mc5 = row.getCell(20) == null ? "" : row.getCell(20).getStringCellValue();
                String fzhs6 = row.getCell(21) == null ? "" : row.getCell(21).getStringCellValue();
                String mc6 = row.getCell(22) == null ? "" : row.getCell(22).getStringCellValue();
                String fzhs7 = row.getCell(23) == null ? "" : row.getCell(23).getStringCellValue();
                String mc7 = row.getCell(24) == null ? "" : row.getCell(24).getStringCellValue();
                String fzhs8 = row.getCell(25) == null ? "" : row.getCell(25).getStringCellValue();
                String mc8 = row.getCell(26) == null ? "" : row.getCell(26).getStringCellValue();
                String fzhs9 = row.getCell(27) == null ? "" : row.getCell(27).getStringCellValue();
                String mc9 = row.getCell(28) == null ? "" : row.getCell(28).getStringCellValue();
                String fzhs10 = row.getCell(29) == null ? "" : row.getCell(29).getStringCellValue();
                String mc10 = row.getCell(30) == null ? "" : row.getCell(30).getStringCellValue();
                String pjh = row.getCell(31) == null ? "" : row.getCell(31).getStringCellValue();
                String pjrq = row.getCell(32) == null ? "" : row.getCell(32).getStringCellValue();
                BtyyCwPzxq pzxq=new BtyyCwPzxq();
                pzxq.setId(UUIDUtils.getId());
                pzxq.setDm(dm);
                pzxq.setZdrq(zdrq);
                pzxq.setPzlb(pzlb);
                pzxq.setPzbh(pzbh);
                pzxq.setFjzs(fjzs);
                pzxq.setCjr(cjr);
                pzxq.setXh(xh);
                pzxq.setKmbh(kmbh);
                pzxq.setKmmc(kmmc);
                pzxq.setZy(zy);
                pzxq.setJfje(jfje);
                pzxq.setDfje(dfje);
                pzxq.setFzhs(fzhs);
                pzxq.setFzhsmc(mc);
                pzxq.setFzhsTwo(fzhs2);
                pzxq.setFzhsmcTwo(mc2);
                pzxq.setFzhsThree(fzhs3);
                pzxq.setFzhsmcThree(mc3);
                pzxq.setFzhsFour(fzhs4);
                pzxq.setFzhsmcFour(mc4);
                pzxq.setFzhsFive(fzhs5);
                pzxq.setFzhsmcFive(mc5);
                pzxq.setFzhsSix(fzhs6);
                pzxq.setFzhsmcSix(mc6);
                pzxq.setFzhsSeven(fzhs7);
                pzxq.setFzhsmcSeven(mc7);
                pzxq.setFzhsEight(fzhs8);
                pzxq.setFzhsmcEight(mc8);
                pzxq.setFzhsNine(fzhs9);
                pzxq.setFzhsmcNine(mc9);
                pzxq.setFzhsTen(fzhs10);
                pzxq.setFzhsmcTen(mc10);
                pzxq.setPjh(pjh);
                pzxq.setPjrq(pjrq);
                pzxqMapper.insert(pzxq);
            }
        }
        return notNull;
    }

}
